package com.xy.template.constant;

import com.xy.system.source.DataSourceType;
import com.xy.system.source.vo.DataSourceVO;
import com.xy.template.model.Attribute;

import java.sql.*;
import java.util.*;

/**
 * 负责获取表的元信息
 * 列的元信息
 */

public class MetadataUtil {


    /**
     * 建立数据库连接
     *
     * @param dataSource
     * @return
     * @throws Exception
     */
    public static Connection openConnection(DataSourceVO dataSource) throws Exception {
        Class.forName(DataSourceType.getByCode(dataSource.getDbType()).getServerName());
        return DriverManager.getConnection(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword());

    }


    /**
     * 列信息数组的集合。List中每个元素是一个数组，代表一个列的信息；
     * 每个数组的元素1是列名，元素2是注释，元素3是类型
     *
     * @return
     */
    public static List<Attribute> getTableColumnsInfo(DataSourceVO dataSource, String tableName) throws Exception {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;
        List<Attribute> columnInfoList = new ArrayList<Attribute>();
        try {
            conn = openConnection(dataSource);
            st = conn.createStatement();
            Map<String, String> remarkMap = DataSourceType.getByCode(dataSource.getDbType()).getRemarkNameMap(conn, dataSource, tableName);
            rs = st.executeQuery("select * from " + tableName);
            ResultSetMetaData rsMetaData = rs.getMetaData();
            String name;
            String type;
            int size;
            int scale;
            for (int index = 0; index < rsMetaData.getColumnCount(); index++) {
                name = rsMetaData.getColumnName(index + 1).toLowerCase();
                type = rsMetaData.getColumnTypeName(index + 1);
                size = rsMetaData.getColumnDisplaySize(index + 1);
                scale = rsMetaData.getScale(index + 1);
                columnInfoList.add(new Attribute(name, JavaNameUtil.dbTypeChangeJavaType(type,scale), remarkMap.get(name), size));
            }
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
        return columnInfoList;
    }

    /**
     * 列信息数组的集合。List中每个元素是一个数组，代表一个列的信息；
     * 每个数组的元素1是列名，元素2是注释，元素3是类型
     *
     * @return
     */
    public static List<String> getTableInfo(DataSourceVO dataSource) throws Exception {
        Connection conn = null;
        try {
            conn = openConnection(dataSource);
            List<String> tableNames = DataSourceType.getByCode(dataSource.getDbType()).getAllTableMap(conn,dataSource);
            Collections.sort(tableNames);
            return tableNames;
        } finally {

            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                }
            }
        }
    }

}